//! Block statement parsing

use super::super::Parser;
use crate::ast::stmt::BlockStmt;
use crate::error::Result;
use crate::lexer::token::Token;

/// Parse block statement
pub fn parse_block_statement(parser: &mut Parser) -> Result<BlockStmt> {
    parser.expect(&Token::LeftBrace)?;

    let mut statements = Vec::new();

    while !parser.check(&Token::RightBrace) {
        // Consume any extra semicolons (from auto-insertion or explicit)
        while parser.consume(&Token::Semicolon)? {}

        if parser.check(&Token::RightBrace) {
            break;
        }

        if let Some(stmt) = super::parse_statement(parser)? {
            statements.push(stmt);
        } else {
            break;
        }
    }

    parser.expect(&Token::RightBrace)?;

    Ok(BlockStmt {
        statements,
        location: parser.current_location(),
    })
}
